
library(tidyverse)
library(stargazer)
library(ggplot2)
library(ggeffects)

data <- readRDS(file.choose()) #Download the data then find it on your machine

###Manipulation Check Models - Table C2
ang<-lm(q26_1~exp_group, data=data)
hope<-lm(q26_2~exp_group, data=data)
pride<-lm(q26_3~exp_group, data=data)
anx<-lm(q26_4~exp_group, data=data)


# Define the desired order for row labels
custom_row_labels <- c("Support, w/Enthusiasm", 
                       "Oppose w/No Emotion", 
                       "Oppose w/Anger")

custom_column_labels <- c("Anger", "Worry", "Hope", "Pride")


# Use stargazer with custom labels
stargazer(ang, anx, hope, pride, dep.var.caption  = "Perceived Emotionality of Newsletter",
          type="text", digits=2, 
          column.labels = custom_column_labels,
          covariate.labels  = custom_row_labels) 

#Percentiles of Emotions About Gun Policy - Table C3
data %>%
  gather(key = "variable", value = "value", q11_1, q11_2, q11_3, q11_4, ent) %>%
  mutate(variable = case_when(
    variable == "q11_1" ~ "Anger",
    variable == "q11_2" ~ "Worry",
    variable == "q11_3" ~ "Hope",
    variable == "q11_4" ~ "Pride", 
    variable == "ent" ~ "Enthusiasm")) %>%
  group_by(variable) %>%
  summarize(mean = round(mean(value, na.rm = TRUE), 1),
            percentile_10 = quantile(value, 0.1, na.rm = TRUE),
            percentile_90 = quantile(value, 0.9, na.rm = TRUE)) %>%
  arrange(match(variable, c("Anger", "Worry", "Hope", "Pride", "Enthusiasm")))


#Models for Experimental Tables and Plots - Table C4
match_x_ang<- lm(q27_1 ~ exp_group*q11_1, data=data)
match_x_ent<- lm(q27_1 ~ exp_group*ent, data=data)

stargazer(match_x_ang, match_x_ent, digits=3, type="text")

###Plotting Results - Anger - Main Paper Figure 3
#Predicted Values for Graphing 
aa_ang<-ggpredict(match_x_ang, terms=c("exp_group [3:4]", "q11_1 [10, 100]"))  
aa_ang

#Plotting Predicted Values
ang <- ggplot(aa_ang, aes(x = factor(x), y = predicted, fill = group)) +
  geom_bar(stat = "identity", width = 0.7, position = position_dodge()) +
  scale_fill_manual(values = c("grey90", "grey65"),
                    breaks = c("10", "100"), 
                    labels = c("Resp. Low Anger", "Resp. High Anger"),
                    guide = guide_legend(direction = "horizontal")) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                linewidth = 0.3, width = 0.2, position = position_dodge(width = 0.7)) +
  labs(x = "Experimental Group", y = "Predicted Affect Level",
       title = "", 
       subtitle = "") +
  theme_minimal(base_size = 13) +
  theme(legend.title = element_blank(),
        legend.position = c(0.05, 1.05), legend.justification = c(0, 1)) +
  scale_x_discrete(labels = c("Unemotional Newsletter", "Angry Newsletter*")) + 
  labs(caption = "*=sig diff at p <.05")

ang

###Plotting Results - Enthusiasm  - Main Paper Figure 4
aa_aff_ent<-ggpredict(match_x_ent, terms=c("exp_group [1:2]", "ent [0, 50]"))  

ent_aff<- ggplot(aa_aff_ent, aes(x = factor(x), y = predicted, fill = group)) +
  geom_bar(stat = "identity", width = 0.7, position = position_dodge()) +
  scale_fill_manual(values = c("grey90", "grey65"),
                    breaks = c("0", "50"), 
                    labels = c("Resp. Low Ent", "Resp. High Ent"),
                    guide = guide_legend(direction = "horizontal")) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high),
                linewidth = 0.3, width = 0.2, position = position_dodge(width = 0.7)) +
  labs(x = "Experimental Group", y = "Predicted Affect Level",
       title = "Predicted Affect for MC", 
       subtitle="Exp Condition X Respondent Enthusiasm") +
  theme_minimal(base_size = 13) +
  theme(legend.title = element_blank()) +
  scale_x_discrete(labels = c("Unemotional Support", "Enthusiasatic Support")) + 
  theme(legend.position = c(0.05, 1.05), legend.justification = c(0, 1)) 

ent_aff
